<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="com.framework.entity.Global"%>
<%@ page import="com.biz.utils.ConfigUtil" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
    String resoursUrl = ConfigUtil.get("RESOURCES_URL");
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="UTF-8">
<title>立即评价</title>
<jsp:include page="/jsp/inc_head.jsp"></jsp:include>
<script type="text/javascript"
	src="<%=resoursUrl%>resources/assets/raty/jquery.raty.js"></script>
<link rel="stylesheet"
	href="<%=resoursUrl%>resources/assets/raty/jquery.raty.css"
	type="text/css">
<style>
.imgdel {
	width: 16px;
	position: absolute;
	right: -7px;
	top: -7px;
}

.addpicinputli {
	position: relative;
}
</style>
</head>
<body style="padding-bottom:60px;">

	<%-- <input type="hidden" id="orderId" value="${orderId}" /> --%>
	<input type="hidden" id="goodsid" value="${goodsid}" />
	<input type="hidden" id="orderDetailId" value="${orderDetailId}" />



	<form id="form" name="form">
		<input type="hidden" id="add_objcetScore" name="pscore.objcetScore" />
		<input type="hidden" id="form_goodsId" name="pscore.goods_id" />
		<input type="hidden" id="form_orderDetailId" name="pscore.order_detail_id" />
		<input type="hidden" id="form_form_info" name="pscore.info" />
		<input type="hidden" id="add_fileids" name="pscore.fileids" value="" />
		<input type="hidden" id="add_no_objectName" value="" /> 
		<input type="hidden" id="toppicid" value="" />
		<!-- 最后一个图片动态id -->
	</form>


	<div class="am-g">
		<div class="am-u-sm-12 am-text-center am-pad-none"
			style="margin:30px 0 8px;">
			<img src="<%=resoursUrl%>resources/assets/images/icon_dppj.png"
				height="45" />

			<div class="am-u-sm-12 am-pad-none corcolor" style="margin-top:5px;">
				商品评价</div>
		</div>

		<!-- 打分项 -->
		<div id="projectHtml"></div>


		<div class="am-u-sm-12 yjsendtime"></div>
		<div class="am-u-sm-12 pltextarea">
			<textarea maxlength="300" id="form_info"
				placeholder="商品是否满意呢？说说您对我们的看法吧(可输入300字)"></textarea>
		</div>
		<div class="am-u-sm-12 pltextarea">
			<ul class="am-u-sm-12 am-pad-none pluls" id="doc-scrollspy-wrapper"
				style="margin-bottom:0;padding:0;">
			</ul>
		</div>
	</div>


	<div class="am-u-sm-12 am-text-center am-pad-none plfixedbtm"
		style="max-width: inherit;">
		<a href="javascript:;" onclick="self.location=document.referrer;">
			<div class="am-u-sm-6 am-pad-none plbtmbtn1">取消</div> </a> <a
			href="javascript:;">
			<div class="am-u-sm-6 am-pad-none plbtmbtn3" onClick="openpop();">
				<!--当评论中没有任何操作的时候这里的class为plbtmbtn2-->
				提交
			</div> </a>
	</div>


	<div class="zzc"></div>
	<!--网络连接失败弹框-->
	<div class="am-u-sm-12 am-pad-none tanchuconf">
		<div class="am-u-sm-12 am-text-center am-pad-none"
			style="margin:25px 0;">
			<img id="msgimg"
				src="<%=resoursUrl%>resources/assets/images/getno.png"
				style="margin:-3px 10px 0 0;" width="30" /> <span id="tc_msg"></span>
		</div>
		<div class="am-u-sm-12 am-text-center am-pad-none popbtmbtn">
			<div class="am-u-sm-12 am-pad-none popbtmbtn3" onClick="closepop();">确定</div>
			<%--<div class="am-u-sm-12 am-pad-none popbtmbtn2"  onclick="self.location=document.referrer;">确定</div>--%>
		</div>
	</div>
	<script src="<%=resoursUrl%>resources/js/jquery/ajaxfileupload.js"
		type="text/javascript"></script>
	<script type="text/javascript">
    //打开关闭弹框
    function closepop() {
        $(".tanchuconf,.zzc").hide();
    }

    var counpicnumber = 1;

var ajaxpath="${pageContext.request.contextPath}";

    //物流送达
    $(".wuliusend > li").click(function () {
        $(".wuliusend > li").removeClass("backred");
        $(this).addClass("backred");
    })


    $(document).ready(function () {
        Orderdf();

        $("#doc-scrollspy-wrapper").html('');
        uploadpic();
    });



    //打分项模块
    function Orderdf() {
        var goodsid='';
        var html = '';
        $.ajax({ //这里使用到Jquery的ajax方法
            type: "POST",
            dataType: "json",
            url: ajaxpath+'/score/queryProjectNew.do',
            data: {goodsid:goodsid},
            success: function (data) {
                var isdateProject = data.isdateProject;
                var queryPscoreDetail = data.queryPscoreDetail;//list

                if (isdateProject == '1') {
                    $.each(queryPscoreDetail, function (i, n) {
                        jsonTotalCount += 1;//合计打分项
                        var tmphtml = '';
                        var min_p = parseInt(this.min_number);
                        var max_p = parseInt(this.max_number);
                        var width_p = max_p * 24;//parseFloat(max_p)*24;

                        tmphtml += '<div class="am-u-sm-12 am-pad-none commentlie">'
                        + '<div class="am-u-sm-4" style="margin-top:4px;">'
                        + this.score_name
                        + '</div>'
                        + '<div class="am-u-sm-8 am-pad-none" id="xzw_starBox' + this.id + '">'
                        + '<div  id="' + this.id + '">'
                        + '<div class="current-rating" id="showb' + this.id + '"></div>'
                        + '</div>'
                        + '</div>';

                        html += tmphtml;

                    });

                    $("#projectHtml").html(html);


                    $.each(queryPscoreDetail, function (i, n) {
                        var score_name=''+this.score_name;
                        $("#" + this.id).raty({
                            number: this.max_number,
                            path: '<%=resoursUrl%>resources/assets/raty/images/',
                            starOff: 'star-off.png',
                            starOn: 'star-on.png',
                            click: function (score, evt) {
                                selectobjcetScore(this.id, score);
                                deleteObjectNameList(score_name);
                            }
                        });

                        //记录需要发放的项目名称
                        saveObjectNameList(score_name);

                    });

                }


            }
        });
    }
    ;



    /////////////////////////////////////////////////////////////////////
var picPath_goods='<%=Global.OSSURL%>/';
		var picCount = 0;//上传图片数
		//上传图片
		function uploadFileForRunPic(obj, objid) {
			$.ajaxFileUpload({
				url : ajaxpath + '/order/doUploadFile.do',
				secureuri : false,
				type : 'post',
				fileElementId : obj,
				dataType : 'text',
				success : function(dataString, status) {
				    var data=JSON.parse(dataString);
					var savemsg = data.picId;
					if (savemsg == null || savemsg == undefined
							|| savemsg == '') {
						alert("图片上传失败");
						return;
					} else {
						//删除旧图片
						var oldpicimg = $("#fileds_" + objid).val();//旧图id
						deletepicList(oldpicimg);

						var hi = picPath_goods + data.path;
						$("#" + obj).parent().css("backgroundImage",
								"url(" + hi + ")");

						$("#fileds_" + objid).val(data.picId); //赋值文件id

						//隐藏占位图
						$("#zhanweiPic_" + objid).hide();
						$("#number_" + objid).html("");

						picCount += 1;

						//保存图片id
						savepicList(data.picId);

						uploadpic();
					}
				},
				error : function(data, status, e) {
					alert(e);
				}
			});
		}

		function uploadpic() {
			if (picCount < 5) {
				var randid = randomString(32);
				$("#toppicid").val(randid);
				var htmlpic = '';
				htmlpic += '<li class="addpicinputli">'
						+ '<a href="javascript:;" class="upload_pic" style="background-size: 100% 100%;">'
						+ '<input type="file" class="fileInput" id="upfileRefundPic_'
						+ randid
						+ '" name="fileToUpload" onChange="uploadFileForRunPic(\'upfileRefundPic_'+ randid+ '\',\''+ randid+ '\');"/>'
						+ '<img src="${pageContext.request.contextPath}/resources/assets/images/uppic1.png" class="uppicpic" id="zhanweiPic_'
						+ randid
						+ '"/><div><span id="number_' + randid + '">1/5</span></div></a>'
						+ '<input type="hidden" id="fileds_' + randid + '"/>'
						+ '</li>';
				$("#doc-scrollspy-wrapper").append(htmlpic);

				unpdateNumber();//更新1/5 ，2/5，3/5
			}
		}

		//更新1/5 ，2/5，3/5
		function unpdateNumber() {
			var numbershow = (picCount + 1) + '/5';//总数-目前图片数
			var objid = $("#toppicid").val();
			var filedid = $("#fileds_" + objid).val();//获取这个占位是否有图片，如果有图片则跳过

			if (filedid == null || filedid == '' || filedid == 'null') {
				$("#number_" + objid).html(numbershow);
			}

		}

		//图片删除
		function imgdel(obj, objid) {
			picCount -= 1;
			var deletefiles = $("#fileds_" + objid).val();
			deletepicList(deletefiles);
			$(obj).parent().remove();

			if (picCount == 4) {
				uploadpic();
			}
		}

		///////////////////开始/////////////////////////////////////////////////////////////////////////

		//保存总打分项目名称，即所有未打分项目
		function saveObjectNameList(objectname) {
			var add_no_objectName = $("#add_no_objectName").val();
			if (add_no_objectName == null || add_no_objectName == '') {
				add_no_objectName += "" + objectname;
			} else {
				add_no_objectName += "," + objectname;
			}
			$("#add_no_objectName").val(add_no_objectName);
		}

		//删除总打分项目名称，即所有未打分项目
		function deleteObjectNameList(objectname) {
			var add_no_objectName = $("#add_no_objectName").val();//总id
			var new_objectName = remove(add_no_objectName, objectname);
			$("#add_no_objectName").val(new_objectName);
		}

		///////////////////结束/////////////////////////////////////////////////////////////////////////

		///////////////////开始/////////////////////////////////////////////////////////////////////////
		//保存图片id
		function savepicList(picid) {
			var add_fileids = $("#add_fileids").val();
			if (add_fileids == null || add_fileids == '') {
				add_fileids += "" + picid;
			} else {
				add_fileids += "," + picid;
			}
			$("#add_fileids").val(add_fileids);
		}
		//删除图片id
		function deletepicList(picid) {
			var add_fileids = $("#add_fileids").val();//总id
			var new_fileids = remove(add_fileids, picid);
			//alert(picid); //alert(new_fileids);
			$("#add_fileids").val(new_fileids);

			unpdateNumber();
		}

		///////////////////结束/////////////////////////////////////////////////////////////////////////
		function remove(a, b) {
			var c = a.split(','), d = "";
			for ( var i = 0; i < c.length; i++) {
				d += c[i] == b ? "" : "," + c[i];
			}
			return d.length > 0 ? d.substring(1) : "";
		}

		//保存打分项id,和分值(objectsortid 打分项,sort 分值)
		function selectobjcetScore(objectsortid, sort) {
			var int_sort = parseInt(sort);
			var string_objectsortid = objectsortid;

			jsonfunction(string_objectsortid, int_sort);

		}

		//保存
		function openpop() {

			var bjcetScore = JSON.stringify(jsonList);//各项打分，逗号分隔

			var goodsid = $("#goodsid").val();
			var orderDetailId = $("#orderDetailId").val();

			var info = $("#form_info").val();//文字评价
			var fileids = $("#add_fileids").val();//图片id，逗号分隔 23432432432,234324324324,23432432

			$("#add_objcetScore").val(bjcetScore);
			$("#form_goodsId").val(goodsid);
			$("#form_orderDetailId").val(orderDetailId);
			$("#form_form_info").val(info);

			if (jsonTotalCount > jsonCount) {

				var add_no_objectName = $("#add_no_objectName").val();

				alert("请为" + q_String(add_no_objectName) + "打分");
				return;
			}

			var html = '';
			$.ajax({ //这里使用到Jquery的ajax方法
				type : "POST",
				url : ajaxpath + '/score/saveScore.do',
				data : $('#form').serialize(),
				async : false,
				success : function(data) {
					var msg = data.msg;

					if (msg == '1') {
						 alert("成功");
						//window.location.href = "${pageContext.request.contextPath}/myOrder/toMyOrderByState.do?flag=0";
					} else {
						//alert("失败");
						$(".tanchuconf,.zzc").show();
						$("#tc_msg").html("保存失败");
					}
				}

			});
		}

		var jsonList = {
			"objcetScore" : []
		};
		var jsonCount = 0;//已打分项
		var jsonTotalCount = 0;//合计总共需要打分项
		function jsonfunction(probjestid, scoredf) {
			//遍历查询是否有相应数据
			var booleanpd = false;//true 存在，false 不存在
			for ( var o in jsonList.objcetScore) {
				if (jsonList.objcetScore[o].Score_project_id == probjestid) {
					//如果存在修改
					jsonList.objcetScore[o].Score = scoredf;
					booleanpd = true;
					break;
				}
			}

			//没有此类数据，则增加
			if (!booleanpd) {
				var arr = {
					"Score_project_id" : probjestid,
					"Score" : scoredf
				}
				jsonCount += 1;
				jsonList.objcetScore.push(arr);
			}
			//alert(JSON.stringify(jsonList));
		}

		//获取逗号分隔，第一个字段
		function q_String(str) {
			arr = str.split(",");
			var ss = '';
			for (x in arr) {
				ss = arr[x];
				break;
			}
			return ss;
		}

		//随机
		function randomString(len) {
			len = len || 32;
			var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
			/****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
			var maxPos = $chars.length;
			var pwd = '';
			for (i = 0; i < len; i++) {
				pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
			}
			return pwd;
		}
	</script>
</body>
</html>
